/*--------------------------------------------------|
| dTree 2.05 | www.destroydrop.com/javascript/tree/ |
|---------------------------------------------------|
| Copyright (c) 2002-2003 Geir Landr?               |
|                                                   |
| This script can be used freely as long as all     |
| copyright messages are intact.                    |
|                                                   |
| Updated: 17.04.2003                               |
|--------------------------------------------------*/

// Node object
function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
	this.id = id;
	this.pid = pid;
	this.name = name;
	this.url = url;
	this.title = title;
	this.target = target;
	this.icon = icon;//+/-
	this.iconOpen = iconOpen;
	this._io = open || false;
	this._is = false;
	this._ls = false;
	this._hc = false;
	this._ai = 0;//in order index
	this._dp = -1;//depth
	this._wd = 200;
	this._tab = 20;
	this._p;
};

// Tree object
function dTree(objName) {
	this.config = {
		target					: null,
		folderLinks			: true,
		useSelection		: true,
		useCookies			: true,
		useLines				: true,
		
		useIcons				: true,	
		useStatusText		: false,
		closeSameLevel	: false,
		inOrder					: false	
	}

	this.icon = {		
		plus				: 'img/200809010820plus.gif',
		minus				: 'img/200809010820minus.gif'		
	};
	this.obj = objName;
	this.aNodes = [];
	this.opNodes = [];
	this.root = new Node(-1,'','root');	
	this.selectedNode = null;
	this.selectedFound = false;
	this.completed = false;
};

// Adds a new node to the node array
dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
	this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
};

// Outputs the tree to the page
dTree.prototype.toString = function() {
	this.root._hc = true;
	var str = '<div class="dtree">\n';
	//str += '<table width="100%" style="border:hidden">';	
	str += this.addNode(this.root);	
	//str += '</table>';	
	str += '</div>';	
	return str;
};

// Creates the tree structure
dTree.prototype.addNode = function(pNode) {
	var str = '';
	var n=0;
	if (this.config.inOrder) n = pNode._ai;
	str += this.node(pNode,pNode.id)
	for (n; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == pNode.id) {
			var cn = this.aNodes[n];
			cn._p = pNode;
			cn._dp = pNode._dp + 1;
			cn._wd = pNode._wd - pNode._tab;
			cn._ai = n;	
			this.setCS(cn);	
			/*
			if (!cn.target && this.config.target) cn.target = this.config.target;
			if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
			if (!this.config.folderLinks && cn._hc) cn.url = null;
			if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
					cn._is = true;
					this.selectedNode = n;
					this.selectedFound = true;
			}
			*/
			str += this.addNode(cn);//recursion
			//if (cn._ls) break;
		}
	}
	return str;
};

// Creates the node icon, url and text
dTree.prototype.node = function(node, nodeId) {
	var str = '';
	if(nodeId == -1 ){
	}
	if(nodeId != -1 && node.pid == 0){
		node._dp = 0;
		node._wd = 160;
		//str += '<tr  style="border:hidden; border:0px"><td id="td'+nodeId+'" style="border:hidden; border:0px">';	
		str += '<div class="MenuLevel1" style="width:'+(node._wd)+'px;" id = "node'+nodeId+'" onclick="javascript:'+this.obj+'.spreadMenu('+nodeId+')">';
		str += '<span id="spanid'+node.id+'" style="display:inline;left:0px;">'+node.name+'</span>';
		str += '<span id="ArrowSubLayerplus'+nodeId+'" style="display:inline;right:0px;">';
		str += '<img src="'+this.icon.plus+'" border="0"/>	';					 
		str += ' </span> ';
		str += ' <span id="ArrowSubLayerminus'+nodeId+'" style="display:none;right:0px;">';
		str += '<img src="'+this.icon.minus+'" border="0"/>';
		str += ' </span></div>';
		//str += '</td></tr>';	
	}
	if(node._hc) {
		//str += '<tr  style="border:hidden; border:0px"><td id="td'+nodeId+'"  style="border:hidden; border:0px">';
		str += '<div class="SubMenuLayerHidden" id = "node'+nodeId+'" onclick="javascript:'+this.obj+'.spreadMenu('+nodeId+')">';
		str += '<span id="spanid'+node.id+'" style="display:inline;left:0px;">'+node.name+'</span>';
		str += '<span id="ArrowSubLayerplus'+nodeId+'" style="display:inline;right:0px;">';
		str += '<img src="'+this.icon.plus+'" border="0"/>	';					 
		str += ' </span> ';
		str += ' <span id="ArrowSubLayerminus'+nodeId+'" style="display:none;right:0px;">';
		str += '<img src="'+this.icon.minus+'" border="0"/>';
		str += ' </span></div>';
		//str += '</td></tr>';
	}else {
		//str += '<tr  style="border:hidden; border:0px"><td  id="td'+nodeId+'"  style="border:hidden; border:0px">';
		str += '<div id="node'+nodeId+'" class="SubMenuLayerHidden">';
		str += '<a class="" href="' + node.url + '" target="'+node.target+'">'+'<span id="spanid'+node.id+'" style="display:inline;left:0px;top:0px;">'+node.name+'</span></a>';
		str += '</div>';
		//str += '</td></tr>';
	}
	
	return str;
};

// Closes all nodes on the same level as certain node
dTree.prototype.closeLevel = function(node) {
	
}

// Closes all nodes 
dTree.prototype.closeAll = function() {
	for (var n=0; n<this.aNodes.length; n++) {
		document.getElementById('node' + this.aNodes[n].id).className = 'SubMenuLayerHidden' ;
		if(this.aNodes[n]._hc) {
				document.getElementById('ArrowSubLayerplus' + nodeId).style.display = '' ;
    			document.getElementById('ArrowSubLayerminus' + nodeId).style.display = 'none' ;
		}
	}
}

// Closes all children of a node
dTree.prototype.closeAllChildren = function(node) {
	for (var n=node.id; n < this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.id) {
			document.getElementById('node' + this.aNodes[n].id).className = 'SubMenuLayerHidden' ;
			if(this.aNodes[n]._hc) {
				document.getElementById('ArrowSubLayerplus' + this.aNodes[n].id).style.display = 'none' ;
    			document.getElementById('ArrowSubLayerminus' + this.aNodes[n].id).style.display = '' ;
    			this.closeAllChildren(this.aNodes[n]);
			}
		}
	}
}

// Open all children of a node
dTree.prototype.openAllChildren = function(node) {
	for (var n=node.id; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.id) {
			//document.getElementById('node' + this.aNodes[n].id).className = 'MenuLevel1' ;
			//document.getElementById('node' + this.aNodes[n].id).class.padding-left += 10px; 
			//document.getElementById('spanid'+this.aNodes[n].id).style.left = (this.aNodes[n]._dp*50)+'px';
			document.getElementById('node' + this.aNodes[n].id).style.left = (this.aNodes[n]._tab*this.aNodes[n]._dp)+'px';
			document.getElementById('node' + this.aNodes[n].id).style.width = (this.aNodes[n]._wd)+'px';
			if(this.aNodes[n]._hc) { 
				document.getElementById('node' + this.aNodes[n].id).className = 'MenuLevel1' ;
				document.getElementById('ArrowSubLayerplus' + this.aNodes[n].id).style.display = '' ;
    			document.getElementById('ArrowSubLayerminus' + this.aNodes[n].id).style.display = 'none' ;
    		//document.getElementById('ArrowSubLayerplus' + this.aNodes[n].id).style.right = (this.aNodes[n]._dp*50)+'px';
			}else {
				document.getElementById('node' + this.aNodes[n].id).className = 'MenuLevel5' ;
				//document.getElementById('spanid'+this.aNodes[n].id).style.left = '100px';
			}
		}
		
	}
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id) {
			//document.getElementById('node' + this.aNodes[n].id).className = 'SubMenuLayerHidden' ;
			if (this.aNodes[n].pid == 0) {
				document.getElementById('ArrowSubLayerplus' + this.aNodes[n].id).style.display = '' ;
				document.getElementById('ArrowSubLayerminus' + this.aNodes[n].id).style.display = 'none' ;
			}
			if (this.aNodes[n]._hc) {
				document.getElementById('ArrowSubLayerplus' + this.aNodes[n].id).style.display = '' ;
				document.getElementById('ArrowSubLayerminus' + this.aNodes[n].id).style.display = 'none' ;
    			this.closeAllChildren(this.aNodes[n]);
			}
		}
	}
}

// Checks if a node has any children and if it is the last sibling
dTree.prototype.setCS = function(node) {
	var lastId;
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.id) node._hc = true;
		if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
	}
	if (lastId==node.id) node._ls = true;
};
dTree.prototype.getOpenNodes = function(nodeId) {
	this.opNodes = [];
	for(var n=0;n<this.aNodes.length;n++) {
			
	}
}
dTree.prototype.spreadMenu = function(nodeId) {
		//closeAll();
	var szSubMenuClassName1 = document.getElementById('node' + nodeId).className ;
	var c_is_op = document.getElementById('ArrowSubLayerminus' + nodeId).style.display;
	if(c_is_op == 'none'){
		//document.getElementById('node' + nodeId).className = 'MenuLevel1' ;
		document.getElementById('ArrowSubLayerplus' + nodeId).style.display = 'none' ;
    	document.getElementById('ArrowSubLayerminus' + nodeId).style.display = '' ;
		this.openAllChildren(this.aNodes[nodeId]);
	}else {
//		/*
		//document.getElementById('node' + nodeId).className = 'SubMenuLayerHidden' ;
		document.getElementById('ArrowSubLayerplus' + nodeId).style.display = '' ;
    	document.getElementById('ArrowSubLayerminus' + nodeId).style.display = 'none' ;
		this.closeAllChildren(this.aNodes[nodeId]);
		
		//document.getElementById('node' + this.aNodes[nodeId].pid).className = 'SubMenuLayerHidden' ;
//		*/
		//this.spreadMenu(this.aNodes[nodeId].pid);
	}
}


/*
// Change the status of a node(open or closed)
dTree.prototype.nodeStatus = function(status, id, bottom) {
	eDiv	= document.getElementById('d' + this.obj + id);
	eJoin	= document.getElementById('j' + this.obj + id);
	if (this.config.useIcons) {
		eIcon	= document.getElementById('i' + this.obj + id);
		eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
	}
	eJoin.src = (this.config.useLines)?
	((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
	((status)?this.icon.nlMinus:this.icon.nlPlus);
	eDiv.style.display = (status) ? 'block': 'none';
};

// [Cookie] Clears a cookie
dTree.prototype.clearCookie = function() {
	var now = new Date();
	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
	this.setCookie('co'+this.obj, 'cookieValue', yesterday);
	this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
};

// [Cookie] Sets value in a cookie
dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
};

// [Cookie] Gets a value from a cookie
dTree.prototype.getCookie = function(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
};

// [Cookie] Returns ids of open nodes as a string
dTree.prototype.updateCookie = function() {
	var str = '';
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
			if (str) str += '.';
			str += this.aNodes[n].id;
		}
	}
	this.setCookie('co' + this.obj, str);
};

// [Cookie] Checks if a node id is in a cookie
dTree.prototype.isOpen = function(id) {
	var aOpen = this.getCookie('co' + this.obj).split('.');
	for (var n=0; n<aOpen.length; n++)
		if (aOpen[n] == id) return true;
	return false;
};
*/

// If Push and pop is not implemented by the browser
if (!Array.prototype.push) {
	Array.prototype.push = function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
};
if (!Array.prototype.pop) {
	Array.prototype.pop = function array_pop() {
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
};